package com.kms.console;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class JSchConsole {
	public String sshExecute(String host, String user, String pwd,String command,int port) throws JSchException, IOException {
		String osName = System.getProperty("os.name");
		// ps -ef|grep tomcat|grep -v grep|awk '{print $2}'
		StringBuffer sb = new StringBuffer();
		java.util.Properties config = new java.util.Properties(); 
		config.put("StrictHostKeyChecking", "no");
			JSch jsch = new JSch();
			if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
				jsch.setKnownHosts("c:\\known_hosts");
			} else {
				jsch.setKnownHosts("/root/.ssh/known_hosts");
			}
			Session session = jsch.getSession(user, host, port);
			session.setPassword(pwd);
			session.setConfig(config);
			session.connect();
			Channel channel = session.openChannel("exec");
			((ChannelExec) channel).setCommand(command);
			InputStream in = channel.getInputStream();
			channel.connect();
			int nextChar;
			while (true) {
				while ((nextChar = in.read()) != -1) {
					sb.append((char) nextChar);
				}
				if (channel.isClosed()) {
					System.out.println("exit-status: "+ channel.getExitStatus());
					sb.append("exit-status: "+ channel.getExitStatus());
					break;
				}
				try {
					Thread.sleep(1000);
				} catch (Exception ee) {
				}
			}
			channel.disconnect();
			session.disconnect();
		return sb.toString();

	}
	
	public static void main(String[] args) throws JSchException, IOException{
		JSchConsole jc = new JSchConsole();
		String host = "127.0.0.1";
		String user = "kms";
		String pwd = "kms";
//		String command = "";
//		command = "cd dpdir;zip exim_kms.zip exim_kms_20110705.dmp";
//		command = "exp kms/kms@kobra file=/home/kms/001.dmp tables=accentry2 log=/home/kms/001.log";
		StringBuffer command = new StringBuffer();
		//linux unix 
//		command.append("exp kms/kms@kobra file=/home/kms/accentry2.dmp tables=accentry2 query="+"\\"+"\"where settledate \\= 20110702"+"\\"+"\" log=/home/kms/accentry2.log ;");
//		command.append("exp kms/kms@kobra file=/home/kms/balance.dmp tables=balance query="+"\\"+"\"where settledate \\= 20110702"+"\\"+"\" log=/home/kms/balance.log ;");
//		command.append("sqlplus system/oracle;");
		
		command.append("df -h ;");
		//windows
//		System.out.println(command.toString());
		int port = 10022;
		
		
		List<String[]> infocommandlist =new ArrayList<String[]>(); 
//		infocommandlist.add(new String[]{"CUP","cat /proc/cpuinfo ;"});
//		infocommandlist.add(new String[]{"Memory","free"});
//		infocommandlist.add(new String[]{"Harddisk","df -h ;"});
//		infocommandlist.add(new String[]{"test :","scp -p /home/kms/kms-2.0.4_20110712.zip root@192.168.8.9:/home/kms ;"});
//		infocommandlist.add(new String[]{"test :","scp -p root@192.168.8.9:/home/kms/kms-2.0.4_20110712.zip  /home/kms/monitor ;"});
//		infocommandlist.add(new String[]{"test :","scp -p root@192.168.8.9:/home/kms/kms-2.0.4_20110712.zip  /home/kms/monitor ;"});
		//压缩备份方式 (expdp kms/kms@kobra directory=dmpdir dumpfile=kms20110803.dmp logfile=kms20110803.log schemas=kms ;)
		
//		infocommandlist.add(new String[]{"test :","expdp kms/kms@89kobra directory=dpdir dumpfile=kms20110803.dmp logfile=kms20110803.log schemas=kms ;"});
//		infocommandlist.add(new String[]{"test :","more /home/oracle/.bash_profile"});
		
		StringBuffer _sb = new StringBuffer();
		infocommandlist.add(new String[]{"t"," cat .bash_profile"});
		String ret = "";
		Map infomap = new HashMap();
		try {
			
			for(String[] infotype:infocommandlist){
//				ret = jc.sshExecute(host, user, pwd, infotype[1], port);
				String[] l = ret.split("\n");
				StringBuffer _s=new StringBuffer();
				for(String r:l){
					if(r.contains("export") || r.contains("DISPLAY") || r.contains("JAVA_HOME") || r.contains("CLASSPATH") || r.contains("PATH") ){
						_s.append(r);
						_s.append("\n");
					}
				}
//				_s.append("expdp kms/kms@kobra directory=dmpdir dumpfile=kms201108039.dmp logfile=kms201108039.log schemas=kms ;");
				
//				ret = jc.sshExecute(host, user, pwd, _s.toString(), port);
				String s = "scp root@192.168.8.10:/home/oracle/dmpdir/exim_kms_20110707.log /home/kms/monotor";
				ret = jc.sshExecute(host, user, pwd, s, port);
			}
		} catch (Exception e) {
			ret = e.toString();
			e.printStackTrace();
		}
	}
}
